import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Events, ModalController } from 'ionic-angular';

import { DeviceDetailPage } from './device-detail/device-detail'
import { DeviceWhPage } from './device-wh/device-wh'
import { DeviceProvider } from '../../providers/service/device'
import { PageModel } from '../../models/page'
import { Keyboard } from '@ionic-native/keyboard';
import { DEFINE } from "../../providers/Constants";
/**
 * Generated class for the DevicePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-device',
    templateUrl: 'device.html',
    providers: [Keyboard]
})
export class DevicePage {

    constructor(public navCtrl: NavController,
        public deviceProvider: DeviceProvider,
        public keyboard: Keyboard,
        public events: Events,
        public modalCtrl: ModalController,
        public navParams: NavParams) {

        //订阅项目更新
        events.subscribe('RF_DEVICE_ITEM', (res) => {
            this.list.map((v, idx) => {
                if (v.id == res.id) {
                    this.list[idx] = res;
                }
            })
        })
    }

    baseUrl = DEFINE.baseURL + '/getFile/';
    isReady = false;
    isLoading = true;
    pageObj = new PageModel();
    searchKey = '';     //编码

    //农场过滤条件
    filterParams = {
        farmId: null,
        farmPanelFlag: false,
        farmName: '全部农场'
    };

    list = [];
    farmArr = [];


    ionViewDidLoad() {
        // console.log('ionViewDidLoad DevicePage');
        this.isReady = true;
        this.getFarmList();
        this.getDataList(true);
        // pageSize    : 10,
        // pageNum     : 1,
        // total       : 1,
        // timeRange   : '',    //时间范围
        // physicalCode: '', //序列号
        // status      : ''     //设备状态
    }

    /**
     * 过滤条件相关
     */
    chooseFilterFarm(id, name){
        if(id == null){
            this.filterParams.farmId = null;
        }else{
            this.filterParams.farmId = id;
        }
        this.filterParams.farmName = name;
        this.filterParams.farmPanelFlag = false;
        this.getDataList(true);
    }
    toggleFarmSearchPanel(){
        this.filterParams.farmPanelFlag = !this.filterParams.farmPanelFlag
    }
    //获取农场
    getFarmList(){
        this.deviceProvider.getFarmList().then((res: any) => {
            this.farmArr = res;
        }, err => {
            console.log(err);
        });
    }

    /**
     * 下拉刷新
     * @param refresher
     */
    doRefresh(refresher) {
        this.getDataList(true).then(() => {
            refresher.complete();
        })
    }

    /**
     * 上拉加载
     * @param infiniteScroll
     */
    doInfinite(infiniteScroll) {
        this.getDataList().then(() => {
            infiniteScroll.complete();
        })
    }

    //搜索
    searchOnHandler() {
        this.getDataList(true);
    }

    //搜索取消
    searchOnCancel(event) {
        event.stopPropagation();
        this.searchOnHandler();
        setTimeout(function () {
            console.log(this.keyboard)
            try {
                this.keyboard || this.keyboard.close(); //关闭keyboard
            } catch (e) { }
            event.target.blur();
        }, 500);
    }

    getDataList(isRefresh = false) {
        this.isLoading = true;
        if (isRefresh) {
            this.pageObj.pageNum = 1;
            this.list = [];
        }

        let elseFilter = {
            physicalCode: this.searchKey,
            farmId: this.filterParams.farmId || 0
        };

        return this.deviceProvider.getList({ ...this.pageObj, ...elseFilter }).then((res: any) => {
            console.log(res.list);
            this.pageObj.pageSize = res.pageSize;
            this.pageObj.pageNum = res.pageNum;
            this.pageObj.total = res.total;
            this.pageObj.isLastPage = res.isLastPage;
            this.pageObj.pageNum++;
            this.isLoading = false;
            if (!res.list) {
                res.list = [];
            }
            this.list = this.list.concat(res.list);
        }, err => {
            this.isLoading = false;
        });

    }

    pToDetail(item) {
        this.navCtrl.push(DeviceDetailPage, { id: item.id });
    }

    pToDeviceAdd(){
        this.navCtrl.push(DeviceWhPage, { type: '1', id: null });     //1  添加设备  2维护设备
    }

}
